package operation;


import book.Book;
import book.BookList;

import java.util.Arrays;
import java.util.Scanner;

public class AddOperation implements IOperation{

    private static void newCapacity(BookList bookList) {
        if(bookList.getUseSize() == bookList.getIntCapacity()) {
            bookList.setBooks(Arrays.copyOf(bookList.getBooks(),bookList.getIntCapacity()*2));
            bookList.setIntCapacity(bookList.getIntCapacity()*2);
        }
    }

    @Override
    public void work(BookList bookList) {
        //判断是否需要扩容
        newCapacity(bookList);

        int currentSize = bookList.getUseSize();
        System.out.println("新增图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字:");
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格:");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);
        bookList.setBook(currentSize,book);
        bookList.setUseSize(currentSize+1);
        System.out.println("添加成功!");
        System.out.println();
    }
}
